package com.example.audiostreaming.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.audiostreaming.R;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;

import java.util.ArrayList;
import java.util.List;

public class AdapterSuggestWordRv extends RecyclerView.Adapter<AdapterSuggestWordRv.InnerHolder> {

    private List<QueryResult> mKeyWordList = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener=null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_search_suggestword_item, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
        holder.setData(mKeyWordList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(mKeyWordList.get(position).getKeyword());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mKeyWordList.size();
    }

    public void setData(List<QueryResult> keyWordList) {
        mKeyWordList.clear();
        mKeyWordList.addAll(keyWordList);
        notifyDataSetChanged();
    }

    public interface OnItemClickListener{
        void onItemClick(String keyword);
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener = onItemClickListener;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(QueryResult queryResult) {
            TextView text = itemView.findViewById(R.id.search_suggestword_item);
            text.setText(queryResult.getKeyword());
        }
    }
}
